//
//  CACustomAnnotationView.m
//  CleanerAir_Project
//
//  Created by 付亚杰 on 16/6/30.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "CACustomAnnotationView.h"
#import "CACustomCalloutView.h"

#define kWidth  20.f
#define kHeight 20.f

#define kHoriMargin 5.f
#define kVertMargin 5.f

#define kPortraitWidth  50.f
#define kPortraitHeight 50.f

#define kCalloutWidth   245.0
#define kCalloutHeight  130.0

@interface CACustomAnnotationView ()

@property (nonatomic, strong) UIImageView *portraitImageView;
@property (nonatomic, strong) UILabel *nameLabel;



@end

@implementation CACustomAnnotationView

@synthesize calloutView;

- (void)calloutViewClick{
    if (self.calloutViewBlock != nil) {
        self.calloutViewBlock(self.info);
    }
}

- (void)setInfo:(Infos *)info{
    if (info != nil) {
        _info = info;
        
        switch (self.info.annotationType) {
            case TYPE_PERSONAL:{
                
                self.portraitImageView.image = [UIImage imageNamed:@"bbb.png"];
                
                break;
            }
            case TYPE_ENTERPRISE:{
                self.portraitImageView.image = [UIImage imageNamed:@"ccc.png"];
                
                break;
                
            }
            case TYPE_DETECTION_POINT:{
                
                if (50 >= info.airpot.integerValue && info.airpot.integerValue > 0) {
                    
                    self.portraitImageView.image = [UIImage imageNamed:@"1-1.png"];
                }else if(100 >= info.airpot.integerValue && info.airpot.integerValue > 50){
                    self.portraitImageView.image = [UIImage imageNamed:@"1-2.png"];
                }else if(150 >= info.airpot.integerValue && info.airpot.integerValue > 100){
                    self.portraitImageView.image = [UIImage imageNamed:@"1-3.png"];
                }else if(200 >= info.airpot.integerValue && info.airpot.integerValue > 150){
                    self.portraitImageView.image = [UIImage imageNamed:@"1-4.png"];
                }else if(300 >= info.airpot.integerValue && info.airpot.integerValue > 200){
                    self.portraitImageView.image = [UIImage imageNamed:@"1-5.png"];
                }else if(info.airpot.integerValue > 300){
                    self.portraitImageView.image = [UIImage imageNamed:@"1-6.png"];
                }
                
                
                break;
            }
            default:
                break;
        }
    }
    
}

- (void)setSelected:(BOOL)selected
{
    [self setSelected:selected animated:NO];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (self.selected == selected)
    {
        return;
    }
    
    if (selected)
    {
        if (self.calloutView == nil)
        {
            /* Construct custom callout. */
            self.calloutView = [[CACustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
                                                  -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y - 3);
            
            
            [self.calloutView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calloutViewClick)]];
            
            CGFloat margin = 10;//左右边距
            CGFloat upMargin = 10;//上边距
//            CGFloat spacing = 10;
            
            CGFloat imageViewX = margin;
            CGFloat imageViewY = upMargin;
            CGFloat imageViewHW = kCalloutHeight - imageViewY * 3;
            

            UIImageView *imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(imageViewX, imageViewY, imageViewHW, imageViewHW))];
            imageView.layer.borderWidth = 0.5f;
            imageView.layer.borderColor = [UIColor grayColor].CGColor;
            imageView.layer.masksToBounds = YES;
            imageView.layer.cornerRadius = imageViewHW * 0.5;
            
            if ([self.info.pic isEqualToString:@""] || self.info.pic == nil) {
                imageView.image = [UIImage imageNamed:@"气泡小图.png"];
            }else{
                
                [imageView sd_setImageWithURL:[NSURL URLWithString:Get_HeadImage_UrlString(self.info.pic)] placeholderImage:[UIImage imageNamed:@"气泡小图.png"]];
            }
            
            
            
            
            CGFloat labelX = CGRectGetMaxX(imageView.frame) + margin - 5;
            CGFloat labelW = kCalloutWidth - imageViewHW - 3 * margin + 5;
            CGFloat labelH = 25;
            CGFloat titleLabelY = upMargin;
            
            
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:(CGRectMake(labelX, titleLabelY, labelW, labelH))];
            titleLabel.textColor = [UIColor grayColor];
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.font = [UIFont systemFontOfSize:12];
            titleLabel.text = self.info.name;
            
            UIView *lineOne = [[UIView alloc] initWithFrame:(CGRectMake(labelX, CGRectGetMaxY(titleLabel.frame) , labelW, 0.5))];
            lineOne.backgroundColor = [UIColor grayColor];
            
            UILabel *AQILabel = [[UILabel alloc] initWithFrame:(CGRectMake(labelX, CGRectGetMaxY(lineOne.frame), labelW, labelH))];
            AQILabel.textAlignment = NSTextAlignmentCenter;
            AQILabel.textColor = [UIColor grayColor];
            AQILabel.font = [UIFont systemFontOfSize:12];
            
            //这句话的意思就是说如果是监测点的话  显示的是 AQI
//            如果不是监测点 显示的是PM2.5
//            先检测 标题也就是 api:  或者  pm2.5 然后检测的是 后面的数据 当model 数据为空的话显示无数       据
            AQILabel.text = [(self.info.annotationType == TYPE_DETECTION_POINT ? @"AQI:" : @"PM2.5:") stringByAppendingString:(self.info.annotationType == TYPE_DETECTION_POINT ? (self.info.airpot == nil ? @"无数据" : self.info.airpot) : (self.info.pm == nil ? @"无数据" : self.info.pm))];
            
            UIView *lineTwo = [[UIView alloc] initWithFrame:(CGRectMake(labelX, CGRectGetMaxY(AQILabel.frame) , labelW, 0.5))];
            lineTwo.backgroundColor = [UIColor grayColor];
            
            UILabel *moodLabel = [[UILabel alloc] initWithFrame:(CGRectMake(labelX, CGRectGetMaxY(lineTwo.frame), labelW, kCalloutHeight - lineTwo.frame.origin.y - 11 - margin))];
            moodLabel.textColor = [UIColor redColor];
            moodLabel.numberOfLines = 0;
            moodLabel.textAlignment = NSTextAlignmentCenter;
            moodLabel.font = [UIFont systemFontOfSize:11];
            moodLabel.text = self.info.mood;
            
            [self.calloutView addSubview:imageView];
            [self.calloutView addSubview:titleLabel];
            [self.calloutView addSubview:lineOne];
            [self.calloutView addSubview:AQILabel];
            [self.calloutView addSubview:lineTwo];
            [self.calloutView addSubview:moodLabel];
            
        }
        
        [self addSubview:self.calloutView];
    }
    else
    {
        [self.calloutView removeFromSuperview];
    }
    
    [super setSelected:selected animated:animated];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL inside = [super pointInside:point withEvent:event];
    /* Points that lie outside the receiver’s bounds are never reported as hits,
     even if they actually lie within one of the receiver’s subviews.
     This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.
     */
    if (!inside && self.selected)
    {
        inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];
    }
    
    return inside;
}

#pragma mark - Life Cycle

- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.bounds = CGRectMake(0.f, 0.f, kWidth, kHeight);
        
        /* Create portrait image view and add to view hierarchy. */
        self.portraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
//        self.portraitImageView.image =
        
        [self addSubview:self.portraitImageView];

    }
    
    return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
